﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Business;
namespace ServicesDirectory.UserControl
{
    public partial class OrganisationDetail2 : System.Web.UI.UserControl
    {
        public string Specialism;
        public string ServicePersonalCircumstances;
        public string ServiceDisability;
        public string ServiceEthnicity;
        public string ServiceBarriers;
        public string Accreditation;
        public string ServiceBenefits;
        /// <summary>
        /// Occurs when [set data].
        /// </summary>
        public event EventHandler SetData;
        /// <summary>
        /// Gets the data.
        /// </summary>
        public void GetData()
        {
            Specialism = GetSelectedValueCheckListBox(cklSpecialism);
            
            ServicePersonalCircumstances = GetSelectedValueCheckListBox(cklPersonal);
            ServiceDisability = GetSelectedValueCheckListBox(cklDisablities);
            ServiceEthnicity = GetSelectedValueCheckListBox(cklEthnicity);
            ServiceBarriers = GetSelectedValueCheckListBox(cklBarriers);
            Accreditation = GetSelectedValueCheckListBox(cklAccreditation);
            ServiceBenefits = GetSelectedValueCheckListBox(cklBenefits);

        }

        /// <summary>
        /// Loads the data.
        /// </summary>
        public void LoadData()
        {
            SetSelectedValueCheckListBox(cklAccreditation, Accreditation);
            SetSelectedValueCheckListBox(cklBarriers, ServiceBarriers);
            SetSelectedValueCheckListBox(cklBenefits, ServiceBenefits);
            SetSelectedValueCheckListBox(cklDisablities, ServiceDisability);
            SetSelectedValueCheckListBox(cklEthnicity, ServiceEthnicity);
            SetSelectedValueCheckListBox(cklPersonal, ServicePersonalCircumstances);
            SetSelectedValueCheckListBox(cklSpecialism, Specialism);
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindOrganisationSpecialisation();
                BindServiceBarriersCapabilities();
                BindServiceBenefitsCapabilities();
                BindServiceDisabilitiesCapabilities();
                BindServiceEthnicityCapabilities();
                BindServicePersonalCircumstanceCapabilities();
                BindAccreditation();
                if (SetData != null)
                    SetData(sender, e);
            }
        }

        #region load reference data
        /// <summary>
        /// Binds the organisation specialisation.
        /// </summary>
        private void BindOrganisationSpecialisation()
        {
            var collSpecialisation = OrganizationBusiness.Instance.GetOrganisationSpecialisation();
            cklSpecialism.DataSource = collSpecialisation;
            cklSpecialism.DataTextField = "DataRef";
            cklSpecialism.DataValueField = "RefValue";
            cklSpecialism.DataBind();
        }

        /// <summary>
        /// Binds the service disabilities capabilities.
        /// </summary>
        private void BindServiceDisabilitiesCapabilities()
        {
            var collection = OrganizationBusiness.Instance.GetServiceDisabilitiesCapabilities();
            cklDisablities.DataSource = collection;
            cklDisablities.DataTextField = "DataRef";
            cklDisablities.DataValueField = "RefValue";
            cklDisablities.DataBind();
        }

        /// <summary>
        /// Binds the service barriers capabilities.
        /// </summary>
        private void BindServiceBarriersCapabilities()
        {
            var collection = OrganizationBusiness.Instance.GetServiceBarriersCapabilities();
            cklBarriers.DataSource = collection;
            cklBarriers.DataTextField = "DataRef";
            cklBarriers.DataValueField = "RefValue";
            cklBarriers.DataBind();
        }

        /// <summary>
        /// Binds the service benefits capabilities.
        /// </summary>
        private void BindServiceBenefitsCapabilities()
        {
            cklBenefits.DataSource = OrganizationBusiness.Instance.GetServiceBenefitsCapabilities();
            cklBenefits.DataTextField = "DataRef";
            cklBenefits.DataValueField = "RefValue";
            cklBenefits.DataBind();
        }

        /// <summary>
        /// Binds the service personal circumstance capabilities.
        /// </summary>
        private void BindServicePersonalCircumstanceCapabilities()
        {
            cklPersonal.DataSource = OrganizationBusiness.Instance.GetServicePersonalCircumstanceCapabilities();
            cklPersonal.DataTextField = "DataRef";
            cklPersonal.DataValueField = "RefValue";
            cklPersonal.DataBind();
        }

        /// <summary>
        /// Binds the service ethnicity capabilities.
        /// </summary>
        private void BindServiceEthnicityCapabilities()
        {
            cklEthnicity.DataSource = OrganizationBusiness.Instance.GetServiceEthnicityCapabilities();
            cklEthnicity.DataTextField = "DataRef";
            cklEthnicity.DataValueField = "RefValue";
            cklEthnicity.DataBind();
        }

        /// <summary>
        /// Binds the accreditation.
        /// </summary>
        private void BindAccreditation()
        {
            cklAccreditation.DataSource = OrganizationBusiness.Instance.GetAccreditation();
            cklAccreditation.DataTextField = "DataRef";
            cklAccreditation.DataValueField = "RefValue";
            cklAccreditation.DataBind();
        }
        /// <summary>
        /// Gets the selected value check list box.
        /// </summary>
        /// <param name="checkBoxList">The check box list.</param>
        /// <returns></returns>
        private string GetSelectedValueCheckListBox(CheckBoxList checkBoxList)
        {
            var result = "";
            foreach (ListItem item in checkBoxList.Items)
            {
                if (item.Selected)
                    result += item.Value + "#";
            }
            return result.Length - 1 > -1 ? result.Substring(0, result.Length - 1) : result;
        }
        /// <summary>
        /// Sets the selected value check list box.
        /// </summary>
        /// <param name="checkBoxList">The check box list.</param>
        /// <param name="values">The values.</param>
        private void SetSelectedValueCheckListBox(CheckBoxList checkBoxList, string values)
        {
            var value = values.Split('#').ToList();
            foreach (ListItem item in checkBoxList.Items)
            {
                if (value.Contains(item.Value))
                    item.Selected = true;
            }

        }
        #endregion
    }
}